#!/usr/bin/env python
import os
import time
file = [ '1.txt' , '2.txt']
back_dir = '/home/k2data/'
backfile = back_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = "zip -qr '%s' %s" % (backfile, ' '.join(file))
if os.system(zip_command)==0:
        print 'success to zip', backfile
else:
        print 'backup file'




#!/usr/bin/env python
class son:
        """docstring for ClassName"""
        def sayhi(self):
                print "My son's name is Qzai"
q = son()
q.sayhi()


class wife(object):
        """docstring for ClassName"""
        def qiweiwei(self):
                print "My wife's name is Qiweiwei"
qi = wife()
qi.qiweiwei()

print " %s is %d " % ('python', 1)

#raw_input
num = raw_input('please enter your number:')
print 'your enter the number is %d' %(int(num)*2)

#!/usr/bin/env python
#read txt
#print ' %s is %s! ' % ('python', 4)
#import sys 
file = open( '1.txt' , 'r')
for line in file:
        print  line,
#print file
#file.close()
#file_object = open('1.txt')
#try:
#     all_the_text = file_object.read()
#finally:
#     file_object.close()
#     
#     

#!/usr/bin/env python
test = ''' this is a \
test input file and output file,
now you can read it easy way!'''
f = file('test.txt', 'w')
f.write(test)
f.close()
f = file('test.txt')
while True:
        line = f.readline()
        if len(line)==0:
                break
        print line,
f.close()

a = 'hello word'
我把a字符串里的word替换为python
1用字符串本身的replace方法
a.replace('word','python')
输出的结果是hello python
2用正则表达式来完成替换:
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
输出的结果也是hello python



#!/usr/bin/env python
import re
inputfile = open('1.txt','r')
outputfile = open('4.txt','w')
while True:
#for line in  inputfile:
  line = inputfile.readline()
  outputfile.write( line.replace('qqq','qiweiwei').replace('jsq','qsj'))
  if len(line)==0:
    break
        #print line,
inputfile.close()
outputfile.close()


#!/usr/bin/env python
class foo:
        '''def Bar(self):
                print 'Bar'
        def Hello(self,name):
                pirnt 'i am test %s' %name
obj = foo()
obj.Bar()
obj.Hello()'''



        def __init__(self,name,age):
                self.name = name
                self.age = age 
        def detail(self):
                print self.name
                print self.age
obj = foo('qiweiwei' , 29)
obj.detail()
#obj1 = foo(qiweiwei,29)
#obj2 = foo(Qzai,0)
#print obj1.name
#print obj1.age
#
#/usr/bin/env python
# -*- coding: utf-8 -*-  
import time, MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="mysql",charset="utf8")    
cursor = conn.cursor()
n = cursor.execute("select Host,user,password from user")      
#print cursor.fetchall()      
for data in cursor.fetchall():
   print  '%s\t%s\t%s' % data     
cursor.close()
import MySQLdb
 
try:
    conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306)
    cur=conn.cursor()
    cur.execute('select * from user')
    cur.close()
    conn.close()
except MySQLdb.Error,e:
     print "Mysql Error %d: %s" % (e.args[0], e.args[1])
#/usr/bin/env python
# -*- coding: utf-8 -*-      
import time, MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="mysql",charset="utf8")
cursor = conn.cursor()
n = cursor.execute("select Host,user,password from user")
m =  cursor.fetchall()
#for data in cursor.fetchall():
for data in m:
   #print  '%s\t%s\t%s' % data     
   print  data[1],data[2]
cursor.close()


#!/usr/bin/env python
#filename:tab.py
#path/usr/lib/python2.7/dist-packages
try:
    import readline
except ImportError:
    print("Module readline not availabe.")
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")


a = [1,2,3,4,5,6,7,8,1,2,3,4,5,6,1,2,3,4,5]
pos = 0
for i in fange(a.count(2)):
    if pos == 0:
        pos = a.index(2)
    else:
        pos = a.index(2,pos+1)
    print pos




#生成随机数验证码
#!/usr/bin/env python
import random
a = []
for i in range(6):
    if i == random.randint(1,5):
        a.append(str(random.randint(1,9)))
    else:
        temp = random.randint(65,90)
        a.append(chr(temp))
print ''.join(a)

#生成验证码
import hashlib
a = raw_input('please input your passwd: ')
hash = hashlib.md5()
#hash.update('admin')
hash.update(a)
print hash.hexdigest()
#print hash.digest()

#序列化
#实现python内存交互
import pickle
qi = ['qiweiwei',11,22,'Qzai','squallqi']
dumpsed = pickle.dumps(qi)
print dumpsed
print type(dumpsed)
loadsed = pickle.loads(dumpsed)
print loadsed
print type(loadsed)
pickle.dump(qi,open('/tmp/pickle.py','w'))
res = pickle.load(open('/tmp/pickle.py','r'))
print result
#json
import json
name_dic = {'name':'qiweiwei','age':'123'}
json.dumps(name_dic)


#正则
import re
起始re.match(pattern,string)
整个re.search('\d+')
re.findall()


装饰器
def outer(fun):
    def wrapper():
        print 'test'
        fun()
        print 'hah'
    return wrapper
